import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Main {

    static int total_line_counter = 0;
    static String temp = "";
    static ArrayList<String> strings = new ArrayList<String>();
    static ArrayList<String> numbers = new ArrayList<String>();
    static ArrayList<String> ops = new ArrayList<String>();

    public static void main(String[] args) throws IOException {

        //
        String[] KeyLists = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"};
        String[] logical = {"&&", "||", "!"};
        String[] arithmetic = {"+", "-", "*", "/", "%", "++", "--"};
        String[] relational = {"==", "!=", "<", ">", "<=", ">="};
        String[] bitwise = {"&", "|", "^", "<<", ">>", "~"};
        String[] assgnment = {"="};
        Set<String> KeyListsSet = new HashSet<String>();
        Set<String> logicalSet = new HashSet<String>();
        Set<String> arithmeticSet = new HashSet<String>();
        Set<String> relationalSet = new HashSet<String>();
        Set<String> bitwiseSet = new HashSet<String>();
        Set<String> assgnmentSet = new HashSet<String>();

        for (int i = 0; i < KeyLists.length; i++) {
            KeyListsSet.add(KeyLists[i]);
        }

        for (int i = 0; i < logical.length; i++) {
            logicalSet.add(logical[i]);
        }
        for (int i = 0; i < arithmetic.length; i++) {
            arithmeticSet.add(arithmetic[i]);
        }
        for (int i = 0; i < relational.length; i++) {
            relationalSet.add(relational[i]);
        }
        for (int i = 0; i < bitwise.length; i++) {
            bitwiseSet.add(bitwise[i]);
        }
        for (int i = 0; i < assgnment.length; i++) {
            assgnmentSet.add(assgnment[i]);
        }
        //
        File file = new File("input_for_lex.c");
        File fileOp = new File("output_of_lexer.txt");
        FileWriter fileWriter = new FileWriter(fileOp);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String curLine = "";
        while (bufferedReader.readLine() != null)
            total_line_counter++;
        bufferedReader = new BufferedReader(new FileReader(file));
        //------------------------------------------------------------------------


        while ((curLine = bufferedReader.readLine()) != null) {
            for (int i = 0; i < curLine.length(); ) {
                if (Character.isLetter(curLine.charAt(i))) {
                    while (Character.isLetter(curLine.charAt(i)) ||
                            Character.isDigit(curLine.charAt(i))
                            ) {
                        temp += curLine.charAt(i);
                        i++;
                    }
                    strings.add(temp);
                    System.out.println("IDtemp = " + temp);
                    //
                    if (logicalSet.contains(temp)) {
                        fileWriter.append("Keywords : " + temp + "\n");
                    } else {
                        fileWriter.append("Identifier : " + temp + "\n");
                    }
                    //
                    temp = "";
                } else if (curLine.charAt(i) == '+' ||
                        curLine.charAt(i) == '-' ||
                        curLine.charAt(i) == '*' ||
                        curLine.charAt(i) == '/' ||
                        curLine.charAt(i) == '=' ||
                        curLine.charAt(i) == '<' ||
                        curLine.charAt(i) == '>') {
                    temp += curLine.charAt(i);
                    i++;
                    ops.add(temp);
                    System.out.println("OPtemp = " + temp);
                    //
                    if (logicalSet.contains(temp)) {
                        fileWriter.append("Logical Operator : " + temp + "\n");
                    } else if (arithmeticSet.contains(temp)) {
                        fileWriter.append("Arithmetic Operator : " + temp + "\n");
                    } else if (relationalSet.contains(temp)) {
                        fileWriter.append("Relational Operator : " + temp + "\n");
                    } else if (bitwiseSet.contains(temp)) {
                        fileWriter.append("Bitwise Operator : " + temp + "\n");
                    } else if (assgnmentSet.contains(temp)) {
                        fileWriter.append("Assignment Operator : " + temp + "\n");
                    }
                    //
                    temp = "";
                } else if (Character.isDigit(curLine.charAt(i))) {
                    while (curLine.charAt(i) == 'e' ||
                            curLine.charAt(i) == 'E' ||/*
                            curLine.charAt(i) == '+' ||*/
                            curLine.charAt(i) == '.' ||
                            Character.isDigit(curLine.charAt(i))
                            ) {
                        temp += curLine.charAt(i);
                        i++;
                    }
                    numbers.add(temp);
                    fileWriter.append("Integer number : " + temp + "\n");
                    System.out.println("NUMtemp = " + temp);
                    temp = "";
                } else if (curLine.charAt(i) == '(' ||
                        curLine.charAt(i) == ')' ||
                        curLine.charAt(i) == '{' ||
                        curLine.charAt(i) == '}') {
                    System.out.println("bracket");
                    fileWriter.append("Bracket : " + curLine.charAt(i) + "\n");
                    i++;
                } else if (curLine.charAt(i) == ';') {
                    System.out.println("delimiter");
                    fileWriter.append("Delimiter : " + curLine.charAt(i) + "\n");
                    fileWriter.append("End : $\n");
                    i++;
                } else i++;
            }
        }
        fileWriter.close();
        //------------------------------------------------------------------------
    }
}